
const App = getApp();
const util = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    last_page:0,
    page:1,
    no_more:false,
    isLoading: true, // 是否正在加载中
    imgPath: "/images",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
   // _this.getPageData();
    _this.setData({
      imgPath:util.getImgPath()
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    _this.getPageData();
  },
	getPageData(isPage, page) {
    let _this = this;
     page = page || 1;
     let res  = {
      code:0,
      typelist:[
        {
          id:1,
          typename:'孕期',
          typeid: 1,
          currentstyle: "active"
        },
        {
         id:2,
         typename:'0-1岁',
         typeid: 2,
         currentstyle: ""
       }
      ],
      row:[
        {
         litpic:'https://demo.eyoucms.com/zy/uploads/allimg/20210107/1-21010G0132R20.jpg',
         title:'宝宝离开妈妈就哭个不停？躲猫猫就能一招解决！',
         seo_description:'各种行销手段早已令人眼花缭乱，但究其本质都是在研究客户（消费', 
         add_time: "2018-06-01",
         typeid: 1,
        },
        {
         litpic:'https://demo.eyoucms.com/zy/uploads/allimg/20210107/1-21010G0132R20.jpg',
         title:'宝宝离开妈妈就哭个不停？躲猫猫就能一招解决！',
         seo_description:'各种行销手段早已令人眼花缭乱，但究其本质都是在研究客户（消费', 
         add_time: "2018-06-01",
         typeid: 1,
        },
        {
         litpic:'https://demo.eyoucms.com/zy/uploads/allimg/20210107/1-21010G0132R20.jpg',
         title:'宝宝离开妈妈就哭个不停？躲猫猫就能一招解决！',
         seo_description:'各种行销手段早已令人眼花缭乱，但究其本质都是在研究客户（消费', 
         add_time: "2018-06-01",
         typeid: 2,
        }
      ]
    }
       
         const api = require("../../api/api")
          api.news.collectionList({
                page: _this.data.page,
                page_size: _this.data.page_size,
                  success(res) {
                   
                    let data =  res.data;   
                    if (data.code == 0) {
                      if (isPage == true) {
                        let dataList = _this.data.list;
                        _this.setData({
                          list: dataList.concat(data.data.rows),
                          last_page:data.data.total,
                          isLoading: false
                        });
                      } else {
                        let list = data.data.rows;
                        _this.setData({
                          list,
                          last_page:data.data.total,
                          isLoading: false
                        });
                      }
                    } else {
                      wx.navigateBack({
                        delta: 1
                      })
                    }
      
                    }

                })

	},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
		let _this = this;
		_this.data.page = 1
		_this.setData({
			page: _this.data.page
    })
		_this.getPageData(); // 获取页面数据
		wx.stopPullDownRefresh(); // 停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this;
		// 已经是最后一页
		if (_this.data.page >= _this.data.last_page) {
			_this.setData({
				no_more: true
			});
			return false;
		}
		// 加载下一页列表
		_this.getPageData(true, ++_this.data.page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  jumpView(e) {
    let aid = e.currentTarget.dataset.id;
    let typeid = e.currentTarget.dataset.typeid;
    let url = `/pages/parenting/article?aid=${aid}&typeid=${typeid}`
     wx.navigateTo({
       url: url
     })
	},
  cancelCollect(e){
    let _this = this,
    aid = e.currentTarget.dataset.aid,
    index = e.currentTarget.dataset.index,
    arr = _this.data.list;
			  App._requestPost(_this,App.globalData.config.apiGetCollectUrl, {
			    aid: aid
			  }, result => {
			    if (result.code === 1) {
            arr.data.splice(index,1);
            this.setData({
              list: arr
            })
			    } else {
			      App.showError(result.msg);
			    }
			  },
			  false,
			  function() {
			    wx.hideLoading();
			  });
  }
})